/**
 *  iBizSys 5.0 机器人生产代码（不要直接修改当前代码）
 *  http://www.ibizsys.net
 */
package com.sa.litetwo.app.card.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.springframework.stereotype.Controller;

import com.sa.litetwo.srv.card.entity.GG_CARD;
import com.sa.litetwo.srv.card.service.GG_CARDService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import net.ibizsys.paas.codelist.ICodeList;
import net.ibizsys.paas.core.IDEField;
import net.ibizsys.paas.data.DataObject;
import net.ibizsys.paas.demodel.DEModelGlobal;
import net.ibizsys.paas.demodel.IDataEntityModel;
import net.ibizsys.paas.entity.IEntity;
import net.ibizsys.paas.service.ServiceGlobal;
import net.ibizsys.paas.sysmodel.CodeListGlobal;
import net.ibizsys.paas.util.StringBuilderEx;
import net.ibizsys.paas.util.StringHelper;
import net.ibizsys.paas.util.spring.IBizOverride;
import net.ibizsys.paas.web.AjaxActionResult;
import net.ibizsys.paas.web.MDAjaxActionResult;
import net.ibizsys.psrt.srv.common.service.FileService;

@Controller
@IBizOverride
public class USRGG_CARDPreController extends GG_CARDPreViewController
{
	public USRGG_CARDPreController() throws Exception
	{
	}
 
	public HashMap<String, Object> getDataInfo()
	{
		HashMap<String, Object> jo = new HashMap<String, Object>();
		try
		{
			String strKey = this.getWebContext().getPostOrParamValue("srfkey");
			if (strKey == null)
				return jo;
			GG_CARD et = new GG_CARD();
			et.setGG_CARDId(strKey);
			GG_CARDService.getInstance().get(et);

			jo = getMap(getDEModel("GG_CARD"), et);

			jo.put("FILENAME", et.getXM() + df2.format(et.getUpdateDate()));

			String strName = "GG_WORK";
			ArrayList GG_WORK = new ArrayList();
			for (IEntity obj : et.getWORK())
			{
				GG_WORK.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_WORK);

			strName = "GG_EDU";
			ArrayList GG_EDU = new ArrayList();
			for (IEntity obj : et.getEDU())
			{
				GG_EDU.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_EDU);

			strName = "GG_GRDHONOR";
			ArrayList GG_GRDHONOR = new ArrayList();
			for (IEntity obj : et.getGRDHONOR())
			{
				GG_GRDHONOR.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_GRDHONOR);

			strName = "GG_CERTIF";
			ArrayList GG_CERTIF = new ArrayList();
			for (IEntity obj : et.getCERTIF())
			{
				GG_CERTIF.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_CERTIF);

			strName = "GG_GRDPOST";
			ArrayList GG_GRDPOST = new ArrayList();
			for (IEntity obj : et.getGRDPOST())
			{
				GG_GRDPOST.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_GRDPOST);

			strName = "GG_SKILL";
			ArrayList GG_SKILL = new ArrayList();
			for (IEntity obj : et.getSKILL())
			{
				GG_SKILL.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_SKILL);

			strName = "GG_ATTACH";
			ArrayList GG_ATTACH = new ArrayList();
			for (IEntity obj : et.getATTACH())
			{
				GG_ATTACH.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_ATTACH);

			strName = "GG_TRAIN";
			ArrayList GG_TRAIN = new ArrayList();
			for (IEntity obj : et.getTRAIN())
			{
				GG_TRAIN.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_TRAIN);

			strName = "GG_PROJ";
			ArrayList GG_PROJ = new ArrayList();
			for (IEntity obj : et.getPROJ())
			{
				GG_PROJ.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_PROJ);

			strName = "GG_OTHER";
			ArrayList GG_OTHER = new ArrayList();
			for (IEntity obj : et.getOTHER())
			{
				GG_OTHER.add(getMap(getDEModel(strName), obj));
			}
			jo.put(strName, GG_OTHER);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return jo;
	}

	public IDataEntityModel getDEModel(String strDENAME)
	{

		IDataEntityModel demodel = null;
		try
		{
			demodel = DEModelGlobal.getDEModel("com.sa.litetwo.srv.card.demodel." + strDENAME + "DEModel");
		}
		catch (Exception ex)
		{
		}

		return demodel;
	}

	private SimpleDateFormat df = new SimpleDateFormat("yyyy/MM");

	private SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private HashMap getMap(IDataEntityModel<IEntity> demodel, IEntity entity)
	{

		HashMap jo = new HashMap();
		try
		{
			Iterator<IDEField> fields = demodel.getDEFields();
			while (fields.hasNext())
			{
				IDEField defield = fields.next();
				if (entity.get(defield.getName()) != null)
				{
					if (defield.getDataType().indexOf("TEXT") >= 0 || defield.getDataType().indexOf("CODELIST") >= 0)
					{
						String strValue = DataObject.getStringValue(entity, defield.getName(), "");

						if (!StringHelper.isNullOrEmpty(defield.getCodeListId()))
						{
							try
							{
								ICodeList codelist = CodeListGlobal.getCodeList(defield.getCodeListId());
								if (codelist != null)
								{
									String strTMP = "";

									strTMP = codelist.getCodeListText(strValue, true);

									if (!StringHelper.isNullOrEmpty(strTMP))
										strValue = strTMP;
								}
							}
							catch (Exception ex)
							{
							}
						}

						jo.put(defield.getName(), strValue);

					}
					else if (defield.getDataType().indexOf("DATE") >= 0 || defield.getDataType().indexOf("TIME") >= 0)
					{

						Timestamp time = DataObject.getTimestampValue(entity, defield.getName(), new Timestamp((new java.util.Date()).getTime()));
						jo.put(defield.getName(), df.format(time));
					}
					else
					{
						String strValue = DataObject.getStringValue(entity, defield.getName(), "");
						jo.put(defield.getName(), strValue);
					}
				}
				else
				{
					jo.put(defield.getName(), "");
					if (defield.getDataType().indexOf("DATE") >= 0 || defield.getDataType().indexOf("TIME") >= 0)
					{
						if (defield.getName().equals("JSSJ"))
						{
							jo.put(defield.getName(), "至今");
						}

					}
				}
			}
		}
		catch (Exception ex)
		{
		}
		return jo;
	}

}